package server;

import client.inventory.IItem;
import client.inventory.ItemLoader;
import client.inventory.MapleInventoryType;
import constants.GameConstants;
import database.BeePool;
import tools.FileoutputUtil;
import tools.Pair;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MTSCart implements Serializable {
    private static long serialVersionUID = 231541893513373578L;
    private int characterId;
    private int tab;
    private int type;
    private int page;
    private List<IItem> transfer;
    private List<Integer> cart;
    private List<Integer> notYetSold;
    private int owedNX;

    public MTSCart(int characterId) throws SQLException {
        this.tab = 1;
        this.type = 0;
        this.page = 0;
        this.transfer = (List<IItem>) new ArrayList();
        this.cart = (List<Integer>) new ArrayList();
        this.notYetSold = (List<Integer>) new ArrayList(10);
        this.owedNX = 0;
        this.characterId = characterId;
        for (Pair<IItem, MapleInventoryType> item : ItemLoader.MTS_TRANSFER.loadItems(false, Integer.valueOf(characterId)).values()) {
            this.transfer.add(item.getLeft());
        }
        this.loadCart();
        this.loadNotYetSold();
    }

    public List<IItem> getInventory() {
        return this.transfer;
    }

    public void addToInventory(IItem item) {
        this.transfer.add(item);
    }

    public void removeFromInventory(IItem item) {
        this.transfer.remove(item);
    }

    public List<Integer> getCart() {
        return this.cart;
    }

    public boolean addToCart(int car) {
        if (!this.cart.contains(Integer.valueOf(car))) {
            this.cart.add(Integer.valueOf(car));
            return true;
        }
        return false;
    }

    public void removeFromCart(int car) {
        for (int i = 0; i < this.cart.size(); ++i) {
            if (((Integer) this.cart.get(i)).intValue() == car) {
                this.cart.remove(i);
            }
        }
    }

    public List<Integer> getNotYetSold() {
        return this.notYetSold;
    }

    public void addToNotYetSold(int car) {
        this.notYetSold.add(Integer.valueOf(car));
    }

    public void removeFromNotYetSold(int car) {
        for (int i = 0; i < this.notYetSold.size(); ++i) {
            if (((Integer) this.notYetSold.get(i)).intValue() == car) {
                this.notYetSold.remove(i);
            }
        }
    }

    public int getSetOwedNX() {
        int on = this.owedNX;
        this.owedNX = 0;
        return on;
    }

    public void increaseOwedNX(int newNX) {
        this.owedNX += newNX;
    }

    public void save() throws SQLException {
        List<Pair<IItem, MapleInventoryType>> itemsWithType = (List<Pair<IItem, MapleInventoryType>>) new ArrayList();
        for (IItem item : this.getInventory()) {
            itemsWithType.add(new Pair(item, GameConstants.getInventoryType(item.getItemId())));
        }
        ItemLoader.MTS_TRANSFER.saveItems(itemsWithType, Integer.valueOf(this.characterId));
        try (Connection con = BeePool.getConnection()) {
            PreparedStatement ps = con.prepareStatement("DELETE FROM mts_cart WHERE characterid = ?");
            ps.setInt(1, this.characterId);
            ps.execute();
            ps.close();
            ps = con.prepareStatement("INSERT INTO mts_cart VALUES(DEFAULT, ?, ?)");
            ps.setInt(1, this.characterId);
            Iterator<Integer> iterator2 = this.cart.iterator();
            while (iterator2.hasNext()) {
                int i = ((Integer) iterator2.next()).intValue();
                ps.setInt(2, i);
                ps.executeUpdate();
            }
            if (this.owedNX > 0) {
                ps.setInt(2, -this.owedNX);
                ps.executeUpdate();
            }
            ps.close();
            
        } catch (SQLException ex) {
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) ex);
        }
    }

    public void loadCart() throws SQLException {
        try (Connection con = BeePool.getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_cart WHERE characterid = ?");
            ps.setInt(1, this.characterId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int iId = rs.getInt("itemid");
                if (iId < 0) {
                    this.owedNX -= iId;
                } else {
                    if (!MTSStorage.getInstance().check(iId)) {
                        continue;
                    }
                    this.cart.add(Integer.valueOf(iId));
                }
            }
            rs.close();
            ps.close();
            
        } catch (SQLException ex) {
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) ex);
        }
    }

    public void loadNotYetSold() throws SQLException {
        try (Connection con = BeePool.getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE characterid = ?");
            ps.setInt(1, this.characterId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int pId = rs.getInt("id");
                if (MTSStorage.getInstance().check(pId)) {
                    this.notYetSold.add(Integer.valueOf(pId));
                }
            }
            rs.close();
            ps.close();
            
        } catch (SQLException ex) {
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) ex);
        }
    }

    public void changeInfo(int tab, int type, int page) {
        this.tab = tab;
        this.type = type;
        this.page = page;
    }

    public int getTab() {
        return this.tab;
    }

    public int getType() {
        return this.type;
    }

    public int getPage() {
        return this.page;
    }
}
